package com.aliang.shopping.model.converter;

import cn.hutool.core.util.StrUtil;
import com.aliang.shopping.model.po.Contact;
import com.aliang.shopping.model.po.Message;
import com.aliang.shopping.model.vo.chat.ContactVO;
import com.aliang.shopping.model.vo.chat.MessageVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.List;

/**
 * 聊天对象数据转换器
 */
@Mapper(componentModel = "Spring")
public interface ChatConverter {

    /**
     * po 转 vo
     *
     * @param message
     * @return
     */
    @Mappings({
            @Mapping(source = "fromId", target = "from.id"),
            @Mapping(source = "toId", target = "to.id")
    })
    MessageVO toVO(Message message);

    List<MessageVO> toMessageVOList(List<Message> list);

    /**
     * vo 转 po
     *
     * @param message
     * @return
     */
    @Mappings({
            @Mapping(target = "fromId", source = "from.id"),
            @Mapping(target = "toId", source = "to.id")
    })
    Message toPO(MessageVO message);


    @Mappings({
            @Mapping(source = "contactId", target = "id")
    })
    ContactVO toContactVO(Contact contact);

    @Mappings({
            @Mapping(source = "id", target = "contactId"),
            @Mapping(target = "lastContent", expression = "java(getLastContent(contactVO))")
    })
    Contact toContactPO(ContactVO contactVO);

    //获取合适的长度
    default String getLastContent(ContactVO contact) {
        if (StrUtil.isBlank(contact.getLastContent())) {
            return "";
        }
        return contact.getLastContent().length() > 50 ? contact.getLastContent().substring(0, 50) + "..." : contact.getLastContent();
    }

    List<ContactVO> toContactVOList(List<Contact> list);
}
